package Ajax;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;

public class CheckerPage implements AjaxPage {

	private boolean mIsRegist = false;
	private String mError = "";

	public CheckerPage(HttpServletRequest request) {
		String option = request.getParameter("option");

		if (option != null) {
			if (option.equalsIgnoreCase("register")) {
				mIsRegist = true;
			}
		}
	}

	@Override
	public void execSql(Statement stmt, HttpServletRequest request)
			throws SQLException {

		String checkname = request.getParameter("checkname");

		if (checkname == null || checkname.isEmpty()) {
			mError = checkname + "_existed";
			return;
		}
	}

	@Override
	public void printXML(PrintWriter writer) throws SQLException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void printHtml(PrintWriter writer) {
		if (!mError.isEmpty()) {
			writer.print("<html><body><script language=\"javascript\" type=\"text/javascript\">parent.valiregister(\"");
			writer.print(mError);
			writer.print("\");</script></body></html>");
		}
	}

	@Override
	public void printDel(PrintWriter writer) {
		writer.print("<html><body>");
		writer.print("certifycode(\"certifyimage_register\", \"wane_register\", \"#\");");
		writer.print("</body></html>");
	}

	private boolean checkUser(Statement stmt, String field) {
		boolean add = false;
		ResultSet rs = null;

		String sql = "SELECT * FROM members WHERE username = '" + field + "'";

		try {
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			if (rs.next()) {
				add =  false;
			} else {
				add = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return add;
	}
}
